package com.boatAnchorAssistant.service.listener;

import com.boatAnchorAssistant.pojo.event.tenant.DeleteTenantEvent;
import com.boatAnchorAssistant.service.service.in.AuthorizationService;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Joseph.Li
 * @author joseph.li@king-v.com
 * Created in 2022-07-17 12:01
 *
 * 权限监听器
 */
@Service
public class AuthorizationListener {

    private final AuthorizationService authorizationService;

    public AuthorizationListener(AuthorizationService authorizationService) {
        this.authorizationService = authorizationService;
    }

    /**
     * 监听删除企业事件
     *
     * 删除企业下所有用户、角色、部门的授权
     * @param event
     */
    @EventListener(DeleteTenantEvent.class)
    public void authorizationListener(DeleteTenantEvent event) {
        List<Long> roleIdList = event.getRoleIdList();
        List<Long> userIdList = event.getUserIdList();
        List<Long> departmentIdList = event.getDepartmentIdList();


        ArrayList<Long> targetIds = new ArrayList<>(roleIdList.size()+ userIdList.size()+departmentIdList.size());

        targetIds.addAll(roleIdList);
        targetIds.addAll(userIdList);
        targetIds.addAll(departmentIdList);

        authorizationService.bathRemoveTargetAuthorized(targetIds);

    }

}
